from django.test import TestCase

# Create your tests here.
def my_send_email():
    """发邮件"""
    # 导入包
    import smtplib
    from email.mime.text import MIMEText

    # 邮件信息-发件人
    sender = "jack_dawson_email@163.com"
    # 邮件信息-收件人
    receiver = "gebidaxiaowang@126.com"
    # 邮件信息-授权码
    password = "ymm123"
    # 邮件信息-主题
    subject = "发邮件0421"
    # 邮件信息-内容
    content = "你好，<a href='http://127.0.0.1:8000/'>单击访问<strong>每日生鲜</strong>首页</a>。"

    # 创建邮件文本对象（内容，类型，编码）
    mime_text = MIMEText(content, "html", "utf-8")
    # 赋值属性
    mime_text["Subject"] = subject
    mime_text["From"] = sender
    mime_text["To"] = receiver

    # 创建smtp对象
    my_smtp = smtplib.SMTP_SSL("smtp.163.com")
    # 登录，账号错误会抛异常
    my_smtp.login(sender, password)
    # 发送邮件(发件人，收件人集合，内容)
    my_smtp.sendmail(sender, [receiver], mime_text.as_string())
    # 关闭
    my_smtp.quit()

    print("发送成功")


def my_send_email_many():
    """发邮件-发个多人"""
    # 导入包
    import smtplib
    from email.mime.text import MIMEText

    # 邮件信息-发件人
    sender = "jack_dawson_email@163.com"
    # 邮件信息-收件人
    receiver1 = "gebidaxiaowang@126.com"
    receiver2 = "278455900@qq.com"
    # 邮件信息-授权码
    password = "ymm123"
    # 邮件信息-主题
    subject = "发邮件0421"
    # 邮件信息-内容
    content = "你好，<a href='http://127.0.0.1:8000/'>单击访问<strong>每日生鲜</strong>首页</a>。"

    # 创建邮件文本对象（内容，类型，编码）
    mime_text = MIMEText(content, "html", "utf-8")
    # 赋值属性
    mime_text["Subject"] = subject
    mime_text["From"] = sender
    mime_text["To"] = ",".join([receiver1,receiver2])#多个收件人使用逗号隔开

    # 创建smtp对象
    my_smtp = smtplib.SMTP_SSL("smtp.163.com", 994)
    # 登录，账号错误会抛异常
    my_smtp.login(sender, password)
    # 发送邮件(发件人，收件人集合，内容)
    my_smtp.sendmail(sender, [receiver1,receiver2], mime_text.as_string())
    # 关闭
    my_smtp.quit()

    print("发送成功")



if __name__ == '__main__':
    my_send_email()